(function($) {

$.fullWidthCarousel = function (element, options){
    var defaults = {
        animationDuration: 1000,
        autoPlay: true,
        durationBetweenAutoSlideChange: 6000,
        showPaddles: options.showPaddles || false,
        showMarqueeButtons: false
    },
    plugin = this,
    $carouselContainer = $(element).closest(options.carouselWrapper),
    $carousel = $(element),
    $slidesList = $(element),
    $slides = $slidesList.children("li"),

    totalSlides = $slides.length,

    autoPlayReference;

    plugin.settings = {};

    //Slide Manager
    var carousel = (function(){
        var totalSlides,slideWidth,
            currentSlideNumber = 1,
            autoPlayRefId;
                
                function init(){
            disableAutoPlay();
            $slides.filter('.cloned').remove();
            $slides = $carouselContainer.find('li');
            slideWidth = $slides.first().outerWidth();
            totalSlides = $slides.length;
            $slidesList.css('width', slideWidth * (totalSlides + 2));
            arrangeSlides();
            if(plugin.settings.autoPlay){
                enableAutoPlay();
            }

        };
        function arrangeSlides(){
            $slides.filter(':first').before($slides.last().clone().addClass('cloned'));
            $slides.filter(':last').after($slides.first().clone().addClass('cloned'));
            $carouselContainer.scrollLeft(slideWidth);
        };

        function enableAutoPlay(){
            autoPlayRefId = window.setInterval(function(){
                goToSlide(currentSlideNumber + 1);
            },plugin.settings.durationBetweenAutoSlideChange);
        };

        function goToSlide(slideNumber,onAnimationCompleteCallback){
            if(currentSlideNumber != slideNumber){
                var dir = slideNumber < currentSlideNumber ? -1 : 1,
                    left = slideWidth * dir;

                $carouselContainer.filter(':not(:animated)').animate({
                    'scrollLeft' : '+=' + left
                },
                {
                    queue:false,
                    duration: plugin.settings.animationDuration,
                    complete:function(){
                        if(onAnimationCompleteCallback){
                            onAnimationCompleteCallback();
                        }
                        else{
                            reArrangeSlides(slideNumber);
                        }
                    }
                });
            }
        };
                function disableAutoPlay(){
                    if(autoPlayRefId)
            clearInterval(autoPlayRefId);
        };
        function reArrangeSlides(slideNumber){

            if (slideNumber == 0) {
              $carouselContainer.scrollLeft(slideWidth * totalSlides);
              slideNumber = totalSlides;
            } else if (slideNumber > totalSlides) {
                $carouselContainer  .scrollLeft(slideWidth);
                slideNumber = 1;
            }
            currentSlideNumber = slideNumber;
        };

        

        return{
            init: init,
        };
    })();

    plugin.init = function(){
        if($slides.length>1){
            plugin.settings = $.extend({},defaults,options);

            carousel.init();
        }
    };

    plugin.init();

}

$.fn.fullWidthCarousel = function(options) {
        return this.each(function() {
            var plugin = new $.fullWidthCarousel(this, options);
            $(this).data('fullWidthCarousel', plugin);
        });

    };

})(jQuery);